package com.xiaoxu.auth.controller;

import com.xiaoxu.auth.model.po.User;
import com.xiaoxu.auth.service.impl.WxAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author xiaoxu
 * @version 1.0
 * @description 微信扫码登录
 * @date 2023/2/2 17:30
 */
@Controller
@Slf4j
public class WxLoginController {

    private final WxAuthService wxAuthService;

    public WxLoginController(WxAuthService wxAuthService) {
        this.wxAuthService = wxAuthService;
    }

    @RequestMapping("/wxLogin")
    public String wxLogin(String code, String state) {
        log.debug("微信扫码回调,code:{},state:{}",code,state);
        //拿授权码申请令牌，查询用户
        User user = wxAuthService.wxAuth(code);
        if(user==null){
            return "redirect:http://www.xuecheng-plus.com/error.html";
        }
        String username = user.getUsername();
        return "redirect:http://www.xuecheng-plus.com/sign.html?username="+username+"&authType=wx";
    }

}
